package com.andy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.andy.db.CoinInfo;
import com.andy.dogcoin.R;

import java.util.List;

/**
 * Created by Andy on  2024/4/24 14:54
 * Email: 963069079@qq.com
 */
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MyViewHolder> {
//    private List<String> mData;
//
//    public MyAdapter(List<String> data) {
//        mData = data;
//    }

    private List<CoinInfo> mList;
    private Context mContext;
    public MainAdapter(List<CoinInfo> mList, Context mContext){
        this.mList=mList;
        this.mContext=mContext;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_coin_item, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textView_nane.setText(mList.get(position).coin_name);
        holder.textView_addr.setText(mList.get(position).addr);

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView textView_nane;
        public TextView textView_addr;
        public LinearLayout allBg;

        public MyViewHolder(View itemView) {
            super(itemView);
            textView_nane = itemView.findViewById(R.id.coin_adapter_item_name);
            textView_addr = itemView.findViewById(R.id.coin_adapter_item_addr);
            allBg = itemView.findViewById(R.id.coin_item_bg);
            allBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }

    private  OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener listenser) {
        onItemClickListener = listenser;
    }
    public interface OnItemClickListener{
        void onItemClick(int position);
    }
}
